<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Questions_model extends CI_Model{
	
	var $table_questions	=	"ans_question";
	var $table_answers		=	"ans_answer";
	var $table_category		=	"ans_category";			
	
	function __construct(){
        parent::__construct(); 
    }
    
    
    function get_questions_list(){
    	$questions	=	$this->db->select()
    					 		 ->where('status', 'yes')
    					 		 ->limit(6)
    					 		 ->order_by('time', 'DESC')
    					 		 ->get($this->table_questions)
    					 		 ->result();
    	if(!empty($questions)){
    		return $questions;
    	} 
    	return false;   					
    }
    
    
    function get_paging_list($perpage, $offset){
    	$paging_list	=	$this->db->select()
    								 ->where('status', 'yes')
    								 ->limit($perpage, $offset)
    								 ->get($this->table_questions)
    								 ->result();
    	if(!empty($paging_list)){
    		return $paging_list;
    	}
    	return false;
    }
    
    
    function get_question_detail($question_link = ""){
    	$question_detail	=	$this->db->select()
    									 ->where('brief_ascii', $question_link)
    									 ->get($this->table_questions)
    									 ->row_array();
    	if(!empty($question_detail)){
    		return $question_detail;
    	}
    	return false;    									 
    }
    
    function get_matching_answers($question_id){
    	$matching_answers	=	$this->db->select()
    									 ->where('id_question', $question_id)
    									 ->where('status', 'yes')
    									 ->order_by('time', 'DESC')
    									 ->get($this->table_answers)
    									 ->row_array();
    	if(!empty($matching_answers)){
    		return $matching_answers;
    	}
    	return false;
    }
    
    
    function get_other_questions($id){
    	$others	=	$this->db->select()
    						 ->where('id !=', $id)
    						 ->where('status', 'yes')
    						 ->limit(6)
    						 ->order_by('time', 'RAND()')
    						 ->get($this->table_questions)
    						 ->result();
    	if(!empty($others)){
    		return $others;
    	}
    	return false;
    }
    
    
    function submit_question($info = array()){
    	$this->db->insert($this->table_questions, $info);
    }
    
    function submit_answer($info = array()){
    	$this->db->insert($this->table_answers, $info);

    }
    
    
    function search_results($key){
    	$results	=	$this->db->like('brief', $key)
    							 ->or_like('detail', $key)
    							 ->where('status', 'yes')
    							 ->order_by('time', 'DESC')
    							 ->get($this->table_questions)
    							 ->result();
    	if(!empty($results)){
    		return $results;
    	}
    	return false;
    }
    
    
    function get_questions_cate(){
    	$questions_cate	=	$this->db->select()
    								 ->get($this->table_category)
    								 ->result();
    	if(!empty($questions_cate)){
    		return $questions_cate;
    	}
    	return false;
    }
    
    
    function get_cate($title_link){
    	$cate	=	$this->db->select()
    	             		 ->where('title_link', $title_link)
    	             		 ->get($this->table_category)
    	             		 ->row_array();
    	if (!empty($cate)){
    		return $cate;
    	}
    	return false;
    }
    
    
    function get_categories($cate_id){
    	$categories	=	$this->db->select()
    							 ->where('cate_id', $cate_id)
    							 ->where('status', 'yes')
    							 ->get($this->table_questions)
    							 ->result();
    	if(!empty($categories)){
    		return $categories;
    	}
    	return false;
    }
    
    
    function get_cate_paging($perpage, $offset, $cate_id){
    	$paging_list	=	$this->db->select()
    								 ->where('cate_id', $cate_id)
    								 ->where('status', 'yes')
    								 ->limit($perpage, $offset)
    								 ->get($this->table_questions)
    								 ->result();
    	if(!empty($paging_list)){
    		return $paging_list;
    	}
    	return false;
    }
}
/* End of file questions_model.php */
/* Location: ./application/models/questions_model.php */